package org.mysterylab.utopiaframework.commons.compass.domain;

import java.io.Serializable;
import java.util.Set;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableProperty;

/**
 * 产品分类目录, 主要是用来展示 Compass 中的 Annotation 的用法.
 * 
 * @author zhouych
 * @see Product
 */
@SuppressWarnings("serial")
@Searchable(alias = "category", root = false)
public class Category implements Serializable {

	/**
	 * 主键ID
	 */
	private long id;
	
	/**
	 * 目录名
	 */
	private String name;
	
	/**
	 * 目录描述
	 */
	private String description;
	
	/**
	 * 该目录的父目录，如：图书-->互联网与网络-->云计算-->《什么是云计算》
	 */
	private Category parentCategory;
	
	/**
	 * 属于该目录下的所有子目录
	 */
	private Set<Category> childCategories;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	// 类别对应搜索的别名为 "cname", 注意对比 AdvanceSearchController.java 中提及的 BookSearchController
	@SearchableProperty(name = "cname")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@SearchableProperty(name = "cdescn")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Category getParentCategory() {
		return parentCategory;
	}

	public void setParentCategory(Category parentCategory) {
		this.parentCategory = parentCategory;
	}

	public Set<Category> getChildCategories() {
		return childCategories;
	}

	public void setChildCategories(Set<Category> childCategories) {
		this.childCategories = childCategories;
	}
	
	/**
	 * 为 Category 添加新的子类别.
	 * 
	 * @param childCategory
	 */
	public void addChildCategory(Category childCategory) {
		if (childCategory == null) {
			throw new IllegalArgumentException("Cannot add a null Category as child.");
		}
		// 删除旧的父类别
		if (childCategory.getParentCategory() != null) {
			childCategory.getParentCategory().getChildCategories().remove(childCategory);
		}
		// 设置新的父类别
		childCategory.setParentCategory(this);
		// 向当前的父类别添加新的子类别
		this.getChildCategories().add(childCategory);
	}
}
